<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MenuRequest;
use App\Model\Admin\SystemMenuModel;
use Illuminate\Http\Request;

class MenuController extends Controller
{
    protected $fields = [
        'pid' => '',
        'title' => '',
        'icon' => '',
        'href' => '',
        'target' => '',
        'sort' => '1',
        'is_menu' => '1',
        'remark' => '',
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.menu.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $select = ['id', 'pid', 'title'];
        // 获取父级列表
        $data['parent'] = $this->getMenuList($select);
        return view('admin.menu.add')->with('data', $data);
    }

    /**
     * 保存信息
     *
     * @param MenuRequest $request
     * @return array
     */
    public function store(MenuRequest $request)
    {
        $menu = new SystemMenuModel();
        foreach ($this->fields as $k => $v){
            $menu -> $k = $request->get($k, $v);
        }

        if ($menu->save()){
            return $this->res_msg(0, '创建成功');
        } else {
            return $this->res_msg(1, '创建失败');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 获取父级列表
        $data['parent'] = $this->getMenuList(['id', 'pid', 'title']);
        // 获取修改的信息
        $Menu = new SystemMenuModel();
        $menu = $Menu->findOrFail($id);
        $data['info'] = ['id'=>$id];
        foreach (array_keys($this->fields) as $v){
            $data['info'][$v] = old($v, $menu->$v);
        }

        return view('admin.menu.edit')->with('data',$data);
    }


    /**
     * 修改信息
     *
     * @param MenuRequest $request
     * @param $id
     * @return array
     */
    public function update(MenuRequest $request, $id)
    {
        $Menu = new SystemMenuModel();
        $menu = $Menu->findOrFail($id);
        foreach ($this->fields as $k => $v){
            $menu -> $k = $request->get($k, $v);
        }

        if ($menu->save()){
            return $this->res_msg(0, '修改成功');
        } else {
            return $this->res_msg(1, '修改失败');
        }
    }

    /**
     * 删除
     *
     * @param $id
     * @return array
     * @throws \Exception
     */
    public function destroy($id)
    {
        //
        $Menu = new SystemMenuModel();
        $menu = $Menu->findOrFail($id);
        if ($menu->delete()){
            return $this->res_msg(0, '删除成功');
        } else {
            return $this->res_msg(1, '删除修改');
        }
    }

    /**
     * 异步返回菜单列表数据
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenu()
    {
        $data['code'] = 0;
        $data['msg'] = '成功';

        $SystemMenu = new SystemMenuModel();
        $data['count'] = $SystemMenu->whereNull('deleted_at')->count();

        $data['data'] = $SystemMenu->whereNull('deleted_at')
            ->select('id', 'pid', 'title', 'icon', 'href', 'target', 'sort', 'status')
            ->orderBy('sort')
            ->get()->toArray();
        return response()->json($data);
    }

    /**
     * 获取系统菜单
     *
     * @param array $select
     * @return array
     */
    public function getMenuList(array $select)
    {
        $SystemMenu = new SystemMenuModel();
        $menu_list = $SystemMenu->whereNull('deleted_at')
            ->select($select)
            ->orderBy('sort')
            ->get()->toArray();

        $menuList = $this->buildMenuChild(0, $menu_list);
        return $menuList;
    }

    /**
     *  生成菜单树
     *
     * @param $pid
     * @param $menu_list
     * @return array
     */
    private function buildMenuChild($pid, $menu_list)
    {
        $tree_list = [];
        foreach ($menu_list as $v) {
            if ($v['pid'] == $pid) {
                $node = (array)$v;

                $child = $this->buildMenuChild($v['id'], $menu_list);

                if (!empty($child)) {
                    $node['child'] = $child;
                }
                $tree_list[] = $node;
            }
        }
        return $tree_list;
    }

    /**
     * 修改状态
     *
     * @param $id
     * @return array
     */
    public function changeStatus($id)
    {
        $Menu = new SystemMenuModel();
        $menu = $Menu->where('id',$id)->first();
        if (empty($menu)){
            return $this->res_msg(1,'没有找到这个项目');
        }

        $menu->status = $menu->status == 1 ? 0 : 1;
        if ($menu->save()){
            return $this->res_msg(0, '修改成功');
        } else {
            return $this->res_msg(1, '修改失败');
        }
    }
}
